package cn.lixin.basic.service.impl;

import cn.lixin.basic.mapper.BaseMapper;
import cn.lixin.basic.query.BaseQuery;
import cn.lixin.basic.service.IBaseService;
import cn.lixin.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
// 不需要service注解，继承类有
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    // 需要注入
    @Autowired
    private  BaseMapper<T> baseMapper;

    /**
     * 添加
     *
     * @param t
     */
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 修改
     *
     * @param t
     */
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 根据id加载
     *
     * @param id
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 加载所有
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 分页查询
     *
     * @param query
     */
    @Override
    public PageList queryPage(BaseQuery query) {
        Integer total = baseMapper.queryCount(query);
        List<T> rows = baseMapper.queryData(query);
        return new PageList(total,rows);
    }

    @Override
    public void removeList(List<Long> ids) {
        baseMapper.removeList(ids);
    }
}
